var loginDialog;
var registrationDialog;
var registrationSuccessfullDialog;

function nicknameClick(){
    
}
function loginClick(){
    loginDialog.dialog("open");    
}
function registerClick(){
    registrationDialog.dialog("open");    
}

$("document").ready(function(){
    loginDialog =  $("#login_dialog_form").
    dialog({
        draggable: false,
        autoOpen: false,
        title: 'Login',
        modal: true,
        resizable: false,
        width: 'auto',
        buttons: [
        {
            text: "Login",            
            click: function(){
                window.location="?request=loggedin&email="+document.forms[1].email.value+"&password="+document.forms[1].password.value;            
            }
        },

        {
            text: "Cancel",
            click: function(){
                $(this).dialog("close");
            }
        }
        ]   
    });
    
    registrationDialog =  $("#registration_dialog_form").
    dialog({
        draggable: false,
        autoOpen: false,
        title: 'Registration',
        modal: true,
        resizable: false,        
        width: 'auto',
        buttons: [
        {
            text: "Register",            
            click: function(){                
                $.getJSON("?request=registred&email="+document.forms["registration"]["email"].value+"&password="+document.forms["registration"]["password"].value+"&nickname="+document.forms["registration"]["nickname"].value+"&securityQuestion="+document.forms["registration"]["securityQuestion"].value+"&securityQuestionAnswer="+document.forms["registration"]["securityQuestionAnswer"].value,
                    function(data){
                        if(data.errorType==null){               
                            $( this ).dialog( "close" );
                            registrationSuccessfullDialog.dialog("open");                            
                        } else {
                            $("#registrationError").html(data.message);                           
                            $( this ).dialog( "close" );
                        }                                
                    });                    
            }
        },
        {
            text: "Cancel",
            click: function(){
                $(this).dialog("close");
            }
        }
        ]   
    });
    registrationSuccessfullDialog=$( "#registration_successful_dialog_form" ).dialog({
        modal: true,
        autoOpen: false,
        buttons: {
            Ok: function() {
                $( this ).dialog( "close" );
            }
        }
    });
});

function validateFormLogin(){
    var vEmail=validateEmail(document.forms["login"]["email"].value);                
    if(vEmail[0]==false)
    {
        document.getElementById("emailError").innerHTML=vEmail[1]; 
        return false;
    }
    else                
        document.getElementById("emailError").innerHTML="";    
    return true;
}